
KernelVersion:=2.6.28.9

KernelSrc:=linux-$(KernelVersion).tar.gz
KernelDir:=linux-$(KernelVersion)

ModuleNam:=e1000
ModuleVer:=7.4.35
ModuleSrc:=$(ModuleNam)-$(ModuleVer).tar.gz
ModuleDir:=$(ModuleNam)-$(ModuleVer)
ModSysDir:=usr/lib/modules/kernel/drivers/net/e1000/ 

ModuleNam2:=r8168
ModuleVer2:=8.017.00
ModuleSrc2:=$(ModuleNam2)-$(ModuleVer2).tar.bz2
ModuleDir2:=$(ModuleNam2)-$(ModuleVer2)
ModSysDir2:=usr/lib/modules/kernel/drivers/net/

ModSysDir3:=usr/lib/modules/kernel/net/

ModTarNam:=module.tgz

IptablesL7ForKernel:=kernel-2.6.25-2.6.28-layer7-2.22.patch
#IptablesL7ForKernel:=kernel-2.6.20-2.6.21-layer7-2.16.1.patch

KerneConfigFile:=default_config

PATCH_KERNEL:=cd $(KernelDir) && patch -p1 < ../$(IptablesL7ForKernel) && cd .. && patch -p0 < encrypt/encrypt_ramdisk_linux_2.6.28.9.patch && patch -p0 < kernel-2.6.28.9-kpcapture.patch
PATCH_E1000:=patch -p0 < e1000-$(ModuleVer).patch;
PATCH_R8168:=patch -p0 < r8168-8.017.00.patch

.PHONY: all clean dist_clean encrypt kpcapture

all:bzImage r8168.ko kpcapture.ko ipt_ipmac.ko ipt_domain.ko module.tgz encrypt 

# We must config system head link to myself.
bzImage:$(KernelSrc) default_config
	tar xzf $(KernelSrc)
	#rm -rf /usr/src/linux 
	#ln `pwd`/$(KernelDir) /usr/src/linux -sf	# Need right of root!
	cp -rf $(KerneConfigFile) $(KernelDir)/.config	
	ln -s $(KernelDir) linux-2.6.28
	$(PATCH_KERNEL)
	#$(MAKE) -C $(KernelDir) oldconfig
	$(MAKE) -C $(KernelDir)
	cp $(KernelDir)/arch/i386/boot/bzImage ./
	#rm -rf /usr/src/linux
	#ln -s /usr/src/linux-2.6.20.7 /usr/src/linux

r8168.ko:$(ModuleSrc2)
	tar -xjf $(ModuleSrc2)
	$(PATCH_R8168)
	cd $(ModuleDir2); make clean modules
	mkdir -p $(ModSysDir2)
	cp $(ModuleDir2)/src/r8168.ko $(ModSysDir2)

e1000.ko:$(ModuleSrc)
	tar -xzf $(ModuleSrc)
	$(PATCH_E1000)
	$(MAKE) -C $(ModuleDir)/src
	cp $(ModuleDir)/src/e1000.ko ./
	mkdir -p $(ModSysDir)
	cp e1000.ko $(ModSysDir)

kpcapture.ko:
	$(MAKE) -C kpcapture
	mkdir -p $(ModSysDir3)
	cp kpcapture/kpcapture.ko $(ModSysDir3)

ipt_ipmac.ko:
	$(MAKE) -C ipt_ipmac
	mkdir -p $(ModSysDir3)
	cp ipt_ipmac/ipt_ipmac.ko $(ModSysDir3)

ipt_domain.ko:
	cd domain && $(MAKE)
	mkdir -p $(ModSysDir3)
	cp domain/ipt_domain.ko $(ModSysDir3)

ipt_time.ko:ipt_time/ipt_time.c ipt_time/ipt_time.h
	$(MAKE) -C ipt_time

module.tgz:default_config 
	$(MAKE) -C $(KernelDir) modules_install INSTALL_MOD_PATH=`pwd`/usr
	#mkdir -p usr/lib/modules/$(KernelVersion)/kernel/net/netfilter/
	#cp ipt_time/ipt_time.ko usr/lib/modules/$(KernelVersion)/kernel/net/netfilter/
	tar -czf $(ModTarNam) usr 

encrypt:
	make -C encrypt

clean:
	rm -rf bzImage module.tgz e1000.ko linux-2.6.28 r8168.ko *.bz2
	make -C encrypt clean
	make -C kpcapture clean

dist_clean:clean
	rm -rf $(KernelDir) $(KernelSrc) $(ModuleSrc) $(ModuleDir) usr

include ../Makefile.Res
